/*
 * MediverMapper.java
 * 
 *  author: Daniel Rudigier
 *    date: 05.06.2008
 * 
 * 
 */
package ocumed.persistenz.mapper;

import java.util.Calendar;
import java.util.List;

import ocumed.persistenz.dao.DAOFactory;
import ocumed.persistenz.dao.MedikamentDAO;
import ocumed.persistenz.dao.MediverDAO;
import ocumed.persistenz.dao.PatientDAO;
import ocumed.persistenz.hibernate.OcMedikament;
import ocumed.persistenz.hibernate.OcMediver;
import ocumed.persistenz.hibernate.OcPatient;
import ocumed.teams.IMediver;

/**
 * @author Daniel Rudigier
 *
 */
public class MediverMapper extends PersistenceMapper {

    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#delete(java.lang.Object)
     */
    @Override
    public void delete(Object instance) {
        MediverDAO mDAO = DAOFactory.get(DAOFactory.RDBMS).getMediverDAO();
        mDAO.start();

        IMediver i = (IMediver) instance;
        OcMediver r = mDAO.findById(i.getMediverid());

        mDAO.delete(r);
        mDAO.commit();
    }

    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#get(int)
     */
    @Override
    public Object get(int id) {
        MediverDAO mDAO = DAOFactory.get(DAOFactory.RDBMS).getMediverDAO();
        mDAO.start();
        return mDAO.findById(id);
    }

    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#getAll()
     */
    @Override
    public List getAll() {
        MediverDAO mDAO = DAOFactory.get(DAOFactory.RDBMS).getMediverDAO();
        mDAO.start();
        return mDAO.findAll();
    }

    /**
     * override
     *
     * @see ocumed.persistenz.mapper.PersistenceMapper#put(java.lang.Object)
     */
    @Override
    public void put(Object instance) {
        MediverDAO mDAO = DAOFactory.get(DAOFactory.RDBMS).getMediverDAO();
        mDAO.start();

        IMediver i = (IMediver) instance;
        OcMediver r = mDAO.findById(i.getMediverid());

        if (r != null) {
            updateMediver(r, i);
            mDAO.attachDirty(r);
        } else {
            r = new OcMediver();
            updateMediver(r, i);
            Integer id = mDAO.persist(r);
            i.setMediverid(id);
        }
        
        mDAO.commit();
    }

    /**
     * @param r
     * @param i
     */
    private void updateMediver(OcMediver r, IMediver i) {
        PatientDAO pDAO = DAOFactory.get(DAOFactory.RDBMS).getPatientDAO();
        MedikamentDAO mDAO = DAOFactory.get(DAOFactory.RDBMS).getMedikamentDAO();
        pDAO.start();
        mDAO.start();
        
        OcPatient p = i.getiPatient() != null ? pDAO.findById(i.getiPatient().getPatientid()) : null;
        OcMedikament m = i.getiMedikament() != null ? mDAO.findById(i.getiMedikament().getMedikamentid()) : null;
        
        r.setMediverid(i.getMediverid());
        r.setOcMedikament(m);
        r.setOcPatient(p);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(i.getMediVerVon());
        r.setMedivervon(c1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(i.getMediVerBis());
        r.setMediverbis(c2);
        r.setMediverdosierung(i.getMediverdosierung());
        r.setMedivermenge((short)i.getMediVerMenge());
    }

}
